#pragma once
#include <utility>
using namespace std;
// 红黑树的颜色定义为枚举类型
enum color
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode
{
	RBTreeNode<K, V>* _parent;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	
	color _col;
	pair<K, V> _kv;
	// 初始化红黑树节点时，传入一个键值对
	RBTreeNode(const pair<K, V>& kv):
		_parent(nullptr),
		_left(nullptr),
		_right(nullptr),
		_col(RED), // 新的节点默认是红色
		_kv(kv)
	{}
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:

	bool insert(const pair<K, V>& kv)
	{
		// 向红黑树中插入节点
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK; // 红黑树的根节点是黑色
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			parent = cur;
			if (kv.first < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else if (kv.first > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		if (kv.first < parent->_kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		// 对插入结果进行调整，当新节点的父亲为黑色时，不需要调整
		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left)
			{
				//     g
			    //   p   u
			    // c
			
				Node* uncle = grandfather->_right;

				// 当叔叔为红色时, 只需要变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					// 继续往上更新
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					// 左左的情况，进行右旋
					if (cur == parent->_left)
					{
						//     g
						//   p
						// c
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					// 左右的情况，进行左右双旋
					else
					{
						// 双旋
						//     g
						//   p
						//     c
						RotateL(parent);
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					break; // 不需要再向上更新了
				}
			}
			else
			{
				//     g
				//   u   p 
				//          c
				//
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					// 右右的情况，进行左旋
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					// 右左的情况，进行右左双旋
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}
	// 左单旋
	void RotateL(Node* parent)
	{
		Node* grandpa = parent->_parent;
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		subR->_left = parent;

		parent->_parent = subR;
		if (subRL) subRL->_parent = parent;

		if (parent == _root)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == grandpa->_left)
			{
				grandpa->_left = subR;
			}
			else
			{
				grandpa->_right = subR;
			}
			subR->_parent = grandpa;
		}
	}
	// 右单旋
	void RotateR(Node* parent)
	{
		Node* grandpa = parent->_parent;
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		subL->_right = parent;

		parent->_parent = subL;
		if (subLR) subLR->_parent = parent;

		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == grandpa->_left)
			{
				grandpa->_left = subL;
			}
			else
			{
				grandpa->_right = subL;
			}
			subL->_parent = grandpa;
		}
	}
	Node* GetRoot() { return _root; }
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << " ";
		_InOrder(root->_right);
	}
private:
	Node* _root = nullptr;
};